<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="参与工程师" prop="helperEngineerCode">
            <el-select v-model="formItem.helperEngineerCode" filterable clearable placeholder="请选择参与工程师">
              <el-option v-for="item in allAvailableFrontlineUsers" :key="item.username" :label="item.name" :value="item.username" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="故障级别" prop="faultLevel">
            <el-select v-model="formItem.faultLevel" placeholder="请选择故障级别">
              <el-option v-for="item in availableFailureLevels" :key="item.code" :label="item.text" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="组长审批意见" prop="approvalComments">
            <el-input v-model="formItem.approvalComments" placeholder="请输入组长审批意见" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div style="float:right">
            <el-button size="mini" @click="save(false,false)">驳回</el-button>
            <el-button v-if="formItem.solved === true" size="mini" @click="save(true,false)">通过</el-button>
            <el-button v-if="formItem.solved === false" :disabled="formItem.helperEngineerCode ? true : false" size="mini" @click="dialogFormVisible = true">转二线</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <FormDialog title="转二线组长" :show="dialogFormVisible" width="30%" :append-to-body="true" @on-ok="ok" @on-cancel="cancel">
      <el-form ref="dialogform" :model="formItem" :rules="rules" label-width="100px" size="mini">
        <el-row :gutter="40">
          <el-col :span="24">
            <el-form-item label="二线组长" prop="secondOperatorCode">
              <el-select v-model="formItem.secondOperatorCode" filterable clearable placeholder="请选择二线组长">
                <el-option v-for="(item) in users" :key="item.username" :label="item.name" :value="item.username" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="转派原因" prop="transferSecondlineReason">
              <el-input v-model="formItem.transferSecondlineReason" placeholder="请输入转派原因" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </FormDialog>
  </div>
</template>

<script>
import { leaderApprovalCreate as create, leaderApprovalDetail as detail } from '@neu/dcom-ui/api/malfunction'
import FormDialog from '@neu/biz-core/components/FormDialog'
import { list as userList } from '@neu/auth-ui/api/user'
import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'

const formItem = {
  // 工作流TaskId
  taskId: '',
  // 工单号
  workNum: '',
  // 审批意见
  approvalComments: '',
  // 操作人编码
  operatorCode: '',
  // 操作人姓名
  operatorName: '',
  // 是否同意
  whetherAgree: false,
  // 是否转二线
  whetherTransfer: false,
  // 一线是否解决
  solved: true,
  // 二线操作人编码
  secondOperatorCode: '',
  // 二线操作人编码名称
  secondOperatorName: '',
  // 参与一线工程师编码
  helperEngineerCode: '',
  // 故障级别
  faultLevel: '',
  // 参与一线工程师名称
  helperEngineerName: '',
  // 转二线原因
  transferSecondlineReason: ''
}

const formRules = {
  approvalComments: [{ required: true, message: '审批意见不能为空', trigger: 'blur' }],
  transferSecondlineReason: [{ required: true, message: '转二线原因不能为空', trigger: 'blur' }],
  secondOperatorCode: [{ required: true, message: '二线操作人不能为空', trigger: 'blur' }]
}

export default {
  name: 'LeaderApprovalForm',
  components: {
    FormDialog
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 故障级别
      availableFailureLevels: [],
      // 二线工程师集合
      users: [],
      // 所有一线工程师集合
      allAvailableFrontlineUsers: [],
      // 弹窗的 控制
      dialogFormVisible: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: { ...formRules }
    }
  },
  created() {
    this.fetch()

    userList({ 's_roleCode': 'worker' }).then(res => {
      this.allAvailableFrontlineUsers = res.data.map(e => { return { name: e.name, username: e.username } })
    })

    // 故障级别
    findByDictionaryType('failureLevel').then(response => {
      this.availableFailureLevels = response.data
    })

    // 二线工程师
    userList({ 's_roleCode': 'expertGroupLeader' }).then(res => {
      this.users = res.data.map(e => { return { name: e.name, username: e.username } })
    })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    save(whetherAgree, whetherTransfer) {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          this.formItem.whetherAgree = whetherAgree
          this.formItem.whetherTransfer = whetherTransfer
          // 清空关于转二线的内容
          if (whetherTransfer === false) {
            this.formItem.secondOperatorCode = ''
            this.formItem.secondOperatorName = ''
            this.formItem.transferSecondlineReason = ''
          }

          // 设置参与工程师
          if (whetherAgree === false && whetherTransfer === false) {
            // 参与工程师
            const helperEngineer = this.allAvailableFrontlineUsers.find(e => e.username === this.formItem.helperEngineerCode)
            this.formItem.helperEngineerName = helperEngineer ? helperEngineer.name : ''
          } else {
            this.formItem.helperEngineerCode = ''
            this.formItem.helperEngineerName = ''
          }

          // 设置工单号和任务
          this.formItem.taskId = this.taskId
          this.formItem.workNum = this.workOrderNum
          // 调用后台
          create(this.formItem).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    },
    // 弹窗确认
    ok() {
      if (!this.formItem.secondOperatorCode) {
        this.$message({
          showClose: true,
          message: '请选择二线工程师',
          type: 'error'
        })
        return false
      }

      if (!this.formItem.transferSecondlineReason) {
        this.$message({
          showClose: true,
          message: '请填写转派原因',
          type: 'error'
        })
        return false
      }

      // 二线工程师名称
      const one = this.users.find(e => e.username === this.formItem.secondOperatorCode)
      this.formItem.secondOperatorName = one ? one.name : ''
      this.save(true, true)
    },
    // 弹窗取消
    cancel() {
      this.dialogFormVisible = false
    }
  }
}
</script>
